package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipInventorySpareRelation;
import cc.comac.mom.bean.EquipInventorySpareRelationBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipInventorySpareRelationDao;
import cc.comac.mom.entity.EquipInventorySpareRelationEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
@Component
@Transactional
public class EquipInventorySpareRelationDataHandler extends ServiceImpl<EquipInventorySpareRelationDao, EquipInventorySpareRelationEntity>{

	@Autowired
	private EquipInventorySpareRelationDao equipInventorySpareRelationDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public boolean batchInsertData (List<EquipInventorySpareRelationEntity> list) {
		boolean saveBatch = false;
		if (list.size() > 0) {
			for (EquipInventorySpareRelationEntity equipInventorySpareRelationEntity : list) {
				if (equipInventorySpareRelationEntity.getInventoryId() == 0) {
					throw new RuntimeException("无法获取盘点数据！");
				}
				if (equipInventorySpareRelationEntity.getSparePartId() == 0) {
					throw new RuntimeException("无法获取备件数据！");
				}
				baseEntityDataUtil.setBaseEntityData(equipInventorySpareRelationEntity);
			}
			saveBatch = saveBatch(list, list.size());
		}
		
		return saveBatch;
	}
	// 通过盘点id分页查询查询盘点对应数据
	public ResultDev selectSpareDataPageByInventotyId (EquipInventorySpareRelation equipInventorySpareRelation ) {
		ResultDev resultDev = new ResultDev();
		if (equipInventorySpareRelation.getPageNum() == 0) {
			equipInventorySpareRelation.setPageNum(1);
		}
		if (equipInventorySpareRelation.getPageSize() == 0) {
			equipInventorySpareRelation.setPageSize(10);
		}
		EquipInventorySpareRelationEntity innerData = equipInventorySpareRelation.getInnerData();
		if (innerData.getInventoryId() == 0) {
			throw new RuntimeException("无法获取盘点信息！");
		}
		equipInventorySpareRelation.setPageNum((equipInventorySpareRelation.getPageNum()-1)*equipInventorySpareRelation.getPageSize());
		List<EquipInventorySpareRelationBean> selectSpareDataBynventotyId = equipInventorySpareRelationDao.selectSpareDataBynventotyId(equipInventorySpareRelation.getPageNum(),equipInventorySpareRelation.getPageSize(),innerData.getInventoryId());
		long selectCountBynventoryId = equipInventorySpareRelationDao.selectCountBynventoryId(innerData.getInventoryId());
		double ceil = Math.ceil((double)selectCountBynventoryId/equipInventorySpareRelation.getPageSize());
		resultDev.setEntityList(selectSpareDataBynventotyId);
		resultDev.setTotalQuantity((int)selectCountBynventoryId);
		resultDev.setPageCount((int) ceil);
		return resultDev;
	}
	
	// 通过盘点id查询对应数据
	public ResultDev selectSpareDataByInventotyId (long inventotyId) {
		ResultDev resultDev = new ResultDev();
		if (inventotyId == 0) {
			throw new RuntimeException("传入盘点单据为空！");
		}
		QueryWrapper<EquipInventorySpareRelationEntity> queryWrapper = new QueryWrapper<EquipInventorySpareRelationEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("inventory_id", inventotyId);
		List<EquipInventorySpareRelationEntity> selectList = equipInventorySpareRelationDao.selectList(queryWrapper);
		resultDev.setEntityList(selectList);
		return resultDev;
	}
	
	public EquipInventorySpareRelationEntity selectById (long id) {
		EquipInventorySpareRelationEntity selectById = equipInventorySpareRelationDao.selectById(id);
		return selectById;
	}
	
	public ResultBean batchDelete(List<EquipInventorySpareRelationEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipInventorySpareRelationEntity> arrayList = new ArrayList<EquipInventorySpareRelationEntity>();
		for (EquipInventorySpareRelationEntity equipInventorySpareRelationEntity : list) {
			EquipInventorySpareRelationEntity equipInventorySpareRelationEntity2 = new EquipInventorySpareRelationEntity();
			equipInventorySpareRelationEntity2.setId(equipInventorySpareRelationEntity.getId());
			equipInventorySpareRelationEntity2.setRowStatus(0);
			baseEntityDataUtil.updateBaseEntityData(equipInventorySpareRelationEntity2);
			arrayList.add(equipInventorySpareRelationEntity2);
		}
		updateBatchById(arrayList, arrayList.size());
		return resultBean;
	}
	
}
